package com.mall.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mall.entity.LoginUser;
import com.mall.entity.SmoothMallUser;
import com.mall.mapper.SmoothMallUserMapper;
import org.apache.ibatis.annotations.Mapper;
import org.checkerframework.checker.i18nformatter.qual.I18nFormat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

@Service
public class UserDetailsServiceImpl implements UserDetailsService {
    @Autowired
    SmoothMallUserMapper smoothMallUserMapper;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        SmoothMallUser smoothMallUser = smoothMallUserMapper.selectOne(new LambdaQueryWrapper<SmoothMallUser>()
                .eq(SmoothMallUser::getUsername, username));
        if (smoothMallUser==null){
            throw  new RuntimeException("用户名不存在");
        }
        //查询用户权限
        Integer role = smoothMallUser.getRole();
        List<String> list=new ArrayList<>();
        if (role==1){
            list.add("user");
        }else {
            Collections.addAll(list,"user","admin");
        }
        return new LoginUser(smoothMallUser,list);
    }
}
